package org.code.generator;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class MpGenerator {

	/**
	 * <p>
	 * MySQL 生成演示
	 * </p>
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	public static void main(String[] args) throws FileNotFoundException, IOException {
		String config = System.getProperty("config", "config.properties");
		Properties properties = new Properties();
		properties.load(new FileInputStream(config));
		AutoGenerator mpg = new AutoGenerator();
		// 全局配置
		GlobalConfig gc = new GlobalConfig();
		gc.setOutputDir(properties.getProperty("output.dir"));
		gc.setFileOverride(true);
		gc.setActiveRecord(false);
		gc.setEnableCache(false);// XML 二级缓存
		gc.setBaseResultMap(true);// XML ResultMap
		gc.setBaseColumnList(true);// XML columList
		gc.setAuthor(properties.getProperty("project.author"));
		// 自定义文件命名，注意 %s 会自动填充表实体属性！
		gc.setMapperName("%sDao");
		gc.setXmlName("%sDao");
		gc.setServiceName("%sService");
		gc.setServiceImplName("%sServiceImpl");
		gc.setControllerName("%sControler");
		TemplateConfig templateConfig = new TemplateConfig();
		templateConfig.setMapper("mapper.vm");
		mpg.setGlobalConfig(gc);
		mpg.setTemplate(templateConfig);
		// 数据源配置
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setDbType(DbType.MYSQL);
		dsc.setDriverName("com.mysql.jdbc.Driver");
		dsc.setUsername(properties.getProperty("db.username"));
		dsc.setPassword(properties.getProperty("db.password"));
		dsc.setUrl(properties.getProperty("db.url"));
		mpg.setDataSource(dsc);
		StrategyConfig strategy = new StrategyConfig();
		strategy.setTablePrefix(properties.getProperty("project.tablePrefix").split(","));//
		strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
		strategy.setInclude(properties.getProperty("project.includeTableNames").split(",")); // 需要生成的表
		String excludeTable = properties.getProperty("project.excludeTableNames");
		if (excludeTable != null && !excludeTable.trim().isEmpty()) {
			strategy.setExclude(excludeTable.split(",")); // 排除生成的表
		}
		mpg.setStrategy(strategy);
		// 包配置
		PackageConfig pc = new PackageConfig();
		pc.setParent(properties.getProperty("project.basePackage"));
		pc.setModuleName(properties.getProperty("project.module"));
		mpg.setPackageInfo(pc);
		// 注入自定义配置，可以在 VM 中使用 cfg.abc 设置的值
		InjectionConfig cfg = new InjectionConfig() {
			@Override
			public void initMap() {
				ConfigBuilder config = this.getConfig();
				config.getPathInfo().put(ConstVal.XML_PATH,
						config.getGlobalConfig().getOutputDir() + File.separatorChar + "mappers");
			}
		};
		mpg.setCfg(cfg);
		mpg.execute();
	}
}